package com.ptkc.ext;

import com.ptkc.core.canal.handler.EventTypeHandler;
import com.ptkc.core.esearch.model.ActivityTarget;
import com.ptkc.core.esearch.repos.ActivitiesRepository;
import com.ptkc.core.esearch.repos.BusinessRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author: Mr.Jiao
 * @date: 2018年12月14日
 */
@Component
public class ActivityTargetCanalHandler extends EventTypeHandler<ActivityTarget> {

//    @Autowired
//    private BusinessRepository businessRepository;

    @Autowired
    private ActivitiesRepository activitiesRepository;

    public ActivityTargetCanalHandler() {
        super("runfast", "runfast_activity_target");
    }

    @Override
    public Class<? extends ActivityTarget> getEntryClass() {
        return ActivityTarget.class;
    }

    @Override
    public void onInsert(ActivityTarget rowData) {
        this.onUpdate(rowData);
    }

    @Override
    public void onUpdate(ActivityTarget rowData) {
        if(rowData != null) {
            if(rowData.getDeleted() != null && rowData.getDeleted()) {
                this.onDelete(rowData);
            }else{
                try {
                    activitiesRepository.saveActivityTarget(rowData);
//                    businessRepository.saveActivityTarget(rowData);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void onDelete(ActivityTarget rowData) {
        try {
            activitiesRepository.delActivityTarget(rowData);
//            if(rowData != null && rowData.getBusinessId() != null && rowData.getId() != null) {
//                businessRepository.delActivityTarget(rowData.getBusinessId(), rowData.getId());
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
